class Solution {
public:
    string multiply(string num1, string num2) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function

        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());

        int n = num1.length();
        int m = num2.length();

        vector<int> result(n + m, 0);
        for (int i = 0; i < n; ++i) {
            int carry = 0;
            for (int j = 0; carry || j < m; ++j) {
                if (j < m) {
                    carry += static_cast<int>(num1[i] - '0') * (num2[j] - '0');
                }
                carry += result[i + j];
                result[i + j] = carry % 10;
                carry /= 10;
            }
        }
        int start = n + m - 1;
        while (start > 0 && result[start] == 0) {
            --start;
        }
        string str;
        while (start >= 0) {
            str += static_cast<char>(result[start] + '0');
            --start;
        }
        return str;
    }
};